package com.dbuil.bleshoes.utils.whiteutil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;

import com.blankj.utilcode.util.LogUtils;

public class Battery {


    public static boolean addWhite(Activity activity){
        boolean isAdd=false;
        PowerManager packageManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
        //应用是否在 白名单中
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (!packageManager.isIgnoringBatteryOptimizations(activity.getPackageName())){

                isAdd=false;
                LogUtils.i("未在优化名单:"+false);
                //方法1、启动一个  ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS Intent
//                Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
//                activity.startActivity(intent);
                //方法2、触发系统对话框
                Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:"+activity.getPackageName()));
                //如果要判断用户是否点击了允许，可以在申请的时候调用 startActivityForResult，然后在 onActivityResult 里再判断一次是否在白名单中。
                activity.startActivityForResult(intent,200);


            }else{

                isAdd=true;
                LogUtils.i("已经在忽略优化名单:"+isAdd);
            }


        }

        return isAdd;
    }


}
